package xyz.eden.cloud.common.web.support.adapter.converter;


import xyz.eden.cloud.common.web.support.adapter.req.PageBaseQryReqVO;
import xyz.eden.cloud.common.web.support.adapter.res.ActionResVO;
import xyz.eden.cloud.common.web.support.adapter.res.CodeDescResVO;
import xyz.eden.cloud.common.web.support.adapter.res.ShowResVO;
import xyz.eden.cloud.common.web.support.application.dto.ActionDTO;
import xyz.eden.cloud.common.web.support.application.dto.CodeDescDTO;
import xyz.eden.cloud.common.web.support.application.dto.PageQryDTO;
import xyz.eden.cloud.common.web.support.application.dto.ShowDTO;

import java.util.Collections;
import java.util.List;
import java.util.Objects;
import java.util.Optional;
import java.util.stream.Collectors;

public class BaseVOConverter {

    public static <T extends PageQryDTO> T builderPageQryDTO(PageBaseQryReqVO vo, T dto) {

        if (Objects.nonNull(dto)) {
            dto.setPageNo(vo.getPageNo());
            dto.setPageSize(vo.getPageSize());
            dto.setKeyword(vo.getKeyword());
        }
        return dto;
    }

    public static <T> List<ActionResVO<T>> builderActionResVO(List<ActionDTO<T>> dtoList) {
        return Optional.ofNullable(dtoList).orElse(Collections.emptyList())
                .stream().map(BaseVOConverter::builderActionResVO).collect(Collectors.toList());
    }

    public static <T> ActionResVO<T> builderActionResVO(ActionDTO<T> dto) {
        return ActionResVO.<T>builder()
                .code(dto.getCode()).key(dto.getKey()).desc(dto.getDesc())
                .build();
    }

    public static <K, V> List<ShowResVO<K, V>> builderShowResVO(List<ShowDTO<K, V>> dtoList) {
        return Optional.ofNullable(dtoList).orElse(Collections.emptyList())
                .stream().map(BaseVOConverter::builderShowResVO).collect(Collectors.toList());
    }

    public static <K, V> ShowResVO<K, V> builderShowResVO(ShowDTO<K, V> dto) {
        return ShowResVO.<K, V>builder().key(dto.getKey()).keyShowDesc(dto.getKeyShowDesc())
                .val(dto.getVal()).vlaShowDesc(dto.getVlaShowDesc())
                .build();
    }

    public static <T> CodeDescResVO<T> builderCodeDescResVO(CodeDescDTO<T> dto) {
        return CodeDescResVO.<T>builder()
                .code(dto.getCode()).desc(dto.getDesc())
                .build();
    }
}
